<?php
class labelModel extends baseModel{

    //解析标签数据
    public function getdata($data){
        $data = stripslashes($data);
        preg_match_all('/([a-z0-9_]+)=[\"|\'](.*)[\"|\']/iU', $data, $matches);
        if(!empty($matches[1])||!empty($matches[2])){
            return array_combine($matches[1], $matches[2]);
        }
    }

    //解析变量
    public function label_variable($data) {
        if(!empty($data['label'])){
            $label = $this->getdata($data['label']);
            $label2 = preg_replace("/\s+/"," ",$data['label']);
            $label2 = explode(' ', $label2);
            $label2 = array_flip($label2);
            $label = array_merge((array)$label,(array)$label2);
        }
        $key = $data['key'];
        $var = $key[$data['var']];

        $html='';
        //裁剪
        if(isset($label['cutout'])){
            $var=$this->getcutout($var,$label['cutout']);
        }
        //时间
        if(isset($label['time'])){
            $var=date($label['time'],$var);
        }
        //为空默认值
        if(isset($label['empty'])){
            if(empty($var)){
                $var=$label['empty'];
            }
        }
        //TAG转换连接
        if(isset($label['tag'])){
            $tag=explode(',', $var); 
            if(!empty($tag)){
                foreach ($tag as $value) {
                    $tags.= '<a href="'.$this->getturl($value).'" title="'.$value.'">'.$value.'</a>,';
                }
                $var=substr($tags, 0,-1);
                unset($tags);
            }
        }
        //字数截取
        if(isset($label['len'])){
            $var=msubstr($var,0,$label['len']);
        }
        //标题样式
        if($data['var']=='titlex'){
            $titlex=$key['title'];
            if(!empty($key['font_color'])){
                $titlex='<font color="'.$key['font_color'].'">'.$titlex.'</font>';
            }
            if($key['font_bold']==1){
                $titlex='<strong>'.$titlex.'</strong>';
            }
            $var=$titlex;
            unset($titlex);
        }
        //html还原
        if(isset($label['html'])){
            $var=html_out($var);
        }

        return $var;
    }

    //解析循环标签
	public function getlist($data) {
        if(empty($data)){
            return;
        }
		$label = $this->getdata($data);
		$table=$label['table'];


		//转换标签
        $label_list=ApiHook('hook_label_list',array('where'=>$where,'label'=>$label));
        $list=array();
        if(!empty($label_list)){
            foreach ($label_list as $value) {
                $list=array_merge((array)$list,(array)$value);
            }
        }
        $table_array=$list[$table];

        if(empty($table_array)){
            return $this->getcommon($where,$label);
        }

        return api($table_array['app'],$table_array['action'],array('data'=>$label));

	}

    //其他表获取
    public function getcommon($where,$data)
    {
        $condition=@substr($where['where'], 4);
        $list=@$this->model->table($data['table'])->where($condition)->limit($where['limit'])->order($where['order'])->select();
        return $list;
    }

    //获取数据统计
    public function getcount($data)
    {
        if(empty($data)){
            return;
        }
        $label = $this->getdata($data);
        return $this->model->table($label['table'])->where($label['where'])->count();
    }

    //表单超链接
    public function getfurl($name)
    {
        return url('form/index/index',array('form'=>$name));
    }

    //TAG超链接
    public function getturl($name)
    {
        return url('tags/index/info',array('name'=>$name));
    }

    //获取原图
    public function get_image($file=null) {

        if(empty($file)){
            return $file;
        }

        if(!file_exists(ROOT_PATH.$file)){
            return $file;
        }
        $info=pathinfo($file);
        $ext=$info['extension'];
        if($ext<>'jpg'&&$ext&&'jpeg'&&$ext<>'png'&&$ext<>'gif'){
            return $file;
        }

        //判断原图
        $name=explode('/', $file);
        $name=end($name);
        if(substr($name,0,6)=='thumb_'){
            $old_name=substr($name,6);
            $file=substr($file, 0,-strlen($name)).$old_name;
        }
        return $file;
    }

    //图片裁剪
    public function getcutout($file=null,$size=null) {

        if(empty($file)||empty($size)){
            return $file;
        }

        if(substr($file,0,4)=='http'){
            return $file;
        }

        $file_url=ROOT_PATH.$file;
        if(!file_exists($file_url)){
            return $file;
        }
        $info=pathinfo($file);
        $ext=$info['extension'];
        if($ext<>'jpg'&&$ext&&'jpeg'&&$ext<>'png'&&$ext<>'gif'){
            return $file;
        }

        $size=explode(',', $size);
        if(empty($size[0])||empty($size[1])){
            return $file;
        }
        $width=intval($size[0]);
        $height=intval($size[1]);

        $file_name=$info['dirname'].'/'.substr($info['basename'], 0,-strlen('.'.$ext));
        $file_dir=ROOT_PATH.$file_name.'_'.$width.'_'.$height.'_cutout'.'.'.$ext;
        $file_url=$file_name.'_'.$width.'_'.$height.'_cutout'.'.'.$ext;

        if(file_exists($file_dir)){
            return $file_url;
        }

        //判断原图
		$name=explode('/', $file);
        $name=end($name);
        if(substr($name,0,6)=='thumb_'){
            $old_size=@getimagesize($file_dir);
            if($old_size[0]<$width||$old_size[1]<$height){
                $old_name=substr($name,6);
                $file=substr($file, 0,-strlen($name)).$old_name;
            }
        }
        
        $thumb=@Image::thumb(ROOT_PATH.'.'.$file,$file_dir,'',$width,$height,'',true);
        if($thumb){
            return $file_url;
        }else{
            return $file;
        }

    }



}